package com.hdu.suixianglu;

import java.util.HashMap;
import java.util.Map;

public class lc454 {
    class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            int ans = 0;
            Map<Integer, Integer> presum2count = new HashMap<>();
            for (int i = 0; i < nums1.length; i++) {
                for (int j = 0; j < nums2.length; j++) {
                    int sum = nums1[i] + nums2[j];
                    presum2count.put(sum, presum2count.getOrDefault(sum, 0) + 1);
                }
            }

            for (int i = 0; i < nums3.length; i++)
            {
                for (int j = 0; j < nums4.length; j++)
                {
                    int sum = nums3[i] + nums4[j];
                    if (presum2count.containsKey(-sum))
                    {
                        ans += presum2count.get(-sum);
                    }
                }
            }

            return ans;
        }
    }
}
